//
//  HomeView.m
//  DaiShengInvest
//
//  Created by 张睿 on 2017/6/28.
//  Copyright © 2017年 davinci. All rights reserved.
//

#import "HomeView.h"
//#import "HomeTopLoginView.h"//xiangfan
#import "HomeTopTransactionView.h"
#import "HomeViewController.h"

@interface HomeView()

@property (nonatomic, weak)UIImageView * backImageView;

@property (nonatomic, strong)HomeTopTransactionView * transacctionTopView;

@property (nonatomic, weak)UILabel * ensureLabel;

@property (nonatomic, weak)UILabel * ensureValueLabel;

@property (nonatomic, weak)UILabel * canUseEnsureLabel;

@property (nonatomic, weak)UILabel * canUseEnsureValueLabel;

@property (nonatomic, weak)UILabel * transactionCountLabel;

@property (nonatomic, weak)UIView * pendingView;

@property (nonatomic, copy)NSString * changeTopImageView;



@property (nonatomic, weak)UIImageView * logoImage;

@property (nonatomic, weak)UILabel * mongeyLabel;

@property (nonatomic, strong)UIButton * showRuleButton;

@property (nonatomic, strong)UILabel * explationLabel;

@property (nonatomic, strong)UIButton * rightImageButton;

@end


@implementation HomeView



- (instancetype)initWithFrame:(CGRect)frame{
    
    if ([super initWithFrame:frame]) {
        
        [self setupUI];
    }
    
    return self;
}



- (void)setChangeButtonImage:(NSString *)changeButtonImage{
    
    _changeButtonImage = changeButtonImage;
    
    if ([changeButtonImage isEqualToString:@"全平"]) {
        
        [self.showRuleButton setTitle:@"查看详情" forState:UIControlStateNormal];
        [self.rightImageButton setImage:[UIImage imageNamed:@"首页平仓"] forState:UIControlStateNormal];
//        self.explationLabel.hidden = NO;
        self.rightImageButton.tag = 2;
    }else{
        
//        [self.showRuleButton setTitle:@"点击查看交易规则" forState:UIControlStateNormal];
        [self.rightImageButton setImage:[UIImage imageNamed:@"入金"] forState:UIControlStateNormal];
//        self.explationLabel.hidden = NO;
        self.rightImageButton.tag = 1;
    }
}

- (void)setTraderMoneyString:(NSString *)traderMoneyString{
    
    _traderMoneyString = traderMoneyString;
    
    
//    float traderMoneyStr = [traderMoneyString floatValue];
    
    self.mongeyLabel.text = traderMoneyString;
}


- (void)setEnsureString:(NSString *)ensureString{
    
    _ensureString = ensureString;
    
    self.ensureValueLabel.text = ensureString;
}


- (void)setCanUseEnsureString:(NSString *)canUseEnsureString{
    
    _canUseEnsureString = canUseEnsureString;
    
    self.canUseEnsureValueLabel.text = canUseEnsureString;
}


- (void)setupUI{
    
    
    NSUserDefaults * userDefaults = [NSUserDefaults standardUserDefaults];
    
    NSString * userID = [userDefaults objectForKey:@"userId"];
    
    if (userID) {
        
        self.changeTopImageView = @"Login";
    }else{
        
        self.changeTopImageView = @"NoLogin";
    }
    
    
    UIImageView * backImageView = [[UIImageView alloc] init];
    
    backImageView.backgroundColor = [UIColor whiteColor];
    backImageView.image = [UIImage imageNamed:@"背景"];
    
    self.backImageView = backImageView;
    
    [self addSubview:self.backImageView];
    
    
    
    
    //    HomeTopLoginView
    
//    self.loginTopView = [HomeTopLoginView new];
//    
//    self.loginTopView.userInteractionEnabled = YES;
//    self.loginTopView.changeImageString = @"全平";
//    self.loginTopView.traderMoneyString = @"0";
//    
//    [self addSubview:self.loginTopView];
    
    self.transacctionTopView = [HomeTopTransactionView new];
    
    [self addSubview:self.transacctionTopView];
    
    if ([self.changeTopImageView isEqualToString:@"NoLogin"]) {
        
        self.transacctionTopView.hidden = NO;
        //        self.loginTopView.hidden = NO;
        
    }else{
    
        self.transacctionTopView.hidden = YES;
        
        UIImageView * logoImage = [[UIImageView alloc] init];
        
//        logoImage.backgroundColor = [UIColor whiteColor];
        logoImage.image = [UIImage imageNamed:@"图层-0"];
        logoImage.contentMode = UIViewContentModeScaleAspectFit;
        
        self.logoImage = logoImage;
        
        [self addSubview:self.logoImage];
        
        
        UIButton * showRuleButton = [[UIButton alloc] init];
        
        showRuleButton.backgroundColor = [UIColor clearColor];
        
        
        [showRuleButton setTitle:@"查看详情" forState:UIControlStateNormal];
        
        showRuleButton.titleLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:10 * SCREENH6];
        [showRuleButton setTitleColor:ColorWithRGB(0xededed) forState:UIControlStateNormal];
        
        [showRuleButton addTarget:[HomeViewController new] action:@selector(didShowRulesButton:) forControlEvents:UIControlEventTouchUpInside];
        
        self.showRuleButton = showRuleButton;
        
        [self addSubview:self.showRuleButton];
        
        
        UILabel * mongeyLabel = [UILabel new];
        
        mongeyLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:24 * SCREENH6];
        mongeyLabel.textColor = ColorWithRGB(0xfd4f4f);
        //    mongeyLabel.text = @"$800,000";
        
        self.mongeyLabel = mongeyLabel;
        
        [self addSubview:self.mongeyLabel];
        
        
        UILabel * explationLabel = [UILabel new];
        
        explationLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:11 * SCREENH6];
        explationLabel.textColor = ColorWithRGB(0x898888);
        explationLabel.text = @"交易账户可用资金";
        
        explationLabel.hidden = NO;
        
        self.explationLabel = explationLabel;
        
        [self addSubview:self.explationLabel];
        
        
        UIButton * rightImageButton = [[UIButton alloc] init];
        
        [rightImageButton setImage:[UIImage imageNamed:@"入金"] forState:UIControlStateNormal];
        
        [rightImageButton addTarget:self action:@selector(didRightImageButton:) forControlEvents:UIControlEventTouchUpInside];
        rightImageButton.tag = 1;
        self.rightImageButton = rightImageButton;
        
        [self addSubview:self.rightImageButton];
    }

    
    
    UILabel * ensureLabel = [UILabel new];
    
    ensureLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:11 * SCREENH6];
    ensureLabel.textColor = ColorWithRGB(0x585c64);
    ensureLabel.text = @"所需保证金$";
    
    self.ensureLabel = ensureLabel;
    
    [self addSubview:self.ensureLabel];
    
    UILabel * ensureValueLabel = [UILabel new];
    
    ensureValueLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:11 * SCREENH6];
    ensureValueLabel.textColor = ColorWithRGB(0x585c64);
    ensureValueLabel.text = @"2.50";
    
    self.ensureValueLabel = ensureValueLabel;
    
    [self addSubview:self.ensureValueLabel];
    
    
    UILabel * canUseEnsureLabel = [UILabel new];
    
    canUseEnsureLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:11 * SCREENH6];
    canUseEnsureLabel.textColor = ColorWithRGB(0x585c64);
    canUseEnsureLabel.text = @"/ 可用保证金$";
    
    self.canUseEnsureLabel = canUseEnsureLabel;
    
    [self addSubview:self.canUseEnsureLabel];
    
    UILabel * canUseEnsureValueLabel = [UILabel new];
    
    canUseEnsureValueLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:11 * SCREENH6];
    canUseEnsureValueLabel.textColor = ColorWithRGB(0x585c64);
    canUseEnsureValueLabel.text = @"500.00";
    
    self.canUseEnsureValueLabel = canUseEnsureValueLabel;
    
    [self addSubview:self.canUseEnsureValueLabel];
    
    UILabel * transactionCountLabel = [UILabel new];
    
    transactionCountLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:14 * SCREENH6];
    transactionCountLabel.textColor = ColorWithRGB(0xecebeb);
    transactionCountLabel.text = @"交易手数";
    
    self.transactionCountLabel = transactionCountLabel;
    
    [self addSubview:self.transactionCountLabel];
    
    
    UIView * pendingView = [UIView new];
    
    pendingView.backgroundColor = ColorWithRGB(0x242424);
    pendingView.layer.borderColor = [ColorWithRGB(0x585c64) CGColor];
    pendingView.layer.borderWidth = 0.5 * SCREENH6;
    pendingView.userInteractionEnabled = YES;
    
     UITapGestureRecognizer *tapGR = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(pushToPendingVC)];
    
    [pendingView addGestureRecognizer:tapGR];
    
    self.pendingView = pendingView;
    
    [self addSubview:self.pendingView];
    
    UIImageView * pendingImage = [UIImageView new];
    
    pendingImage.image = [UIImage imageNamed:@"挂单"];
    
    [self.pendingView addSubview:pendingImage];
    
    [pendingImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.width.mas_equalTo(20 * SCREENH6);
        make.centerY.mas_equalTo(self.pendingView);
        make.leading.mas_equalTo(self.pendingView).offset(10 * SCREENH6);
    }];
    
    UILabel * pendingLabel = [UILabel new];
    
    pendingLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:15 * SCREENH6];
    pendingLabel.textColor = [UIColor whiteColor];//ColorWithRGB(0x585c64);
    pendingLabel.text = @"挂单";
    
    [self.pendingView addSubview:pendingLabel];
    
    [pendingLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.pendingView);
        make.trailing.mas_equalTo(self.pendingView).offset(- 10 * SCREENH6);
    }];
}



- (void)layoutSubviews{
    
    [self.backImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.top.bottom.mas_equalTo(self);
    }];
    
    //loginTopView
    
//    [self.loginTopView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.leading.trailing.mas_equalTo(self);
//        make.height.mas_equalTo(100 * SCREENH6);
//        make.top.mas_equalTo(64 * SCREENH6);
//    }];
    
    [self.logoImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.mas_equalTo(75 * SCREENH6);
        make.leading.mas_equalTo(self).offset(25 * SCREENH6);
        make.top.mas_equalTo(70 * SCREENH6);
    }];
    
    [self.showRuleButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self);
        make.top.mas_equalTo(self.logoImage);
    }];
    
    [self.mongeyLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.showRuleButton.mas_bottom).offset(-5 * SCREENH6);
        make.centerX.mas_equalTo(self);
    }];
    
    [self.explationLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.mongeyLabel.mas_bottom);
        make.centerX.mas_equalTo(self);
    }];
    
    [self.rightImageButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.mas_equalTo(self).offset(-20 * SCREENH6);
        make.top.mas_equalTo(self.logoImage);
        make.width.height.mas_equalTo(75 * SCREENH6);
    }];
    
    [self.transacctionTopView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.mas_equalTo(self);
        make.height.mas_equalTo(100 * SCREENH6);
        make.top.mas_equalTo(64 * SCREENH6);
    }];
    
    [self.ensureLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self).offset(-50 * SCREENH6);
        make.top.mas_equalTo(self).offset(528 * SCREENH6);
    }];//ensureValueLabel
    
    [self.ensureValueLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.ensureLabel.mas_trailing);
        make.top.mas_equalTo(self.ensureLabel);
    }];
    
    [self.canUseEnsureLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.ensureValueLabel.mas_trailing).offset(2 * SCREENH6);
        make.top.mas_equalTo(self.ensureValueLabel);
    }];
    
    [self.canUseEnsureValueLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.canUseEnsureLabel.mas_trailing);
        make.top.mas_equalTo(self.canUseEnsureLabel);
    }];
    
    [self.transactionCountLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self).offset(33 * SCREENH6);
        make.top.mas_equalTo(self).offset(490 * SCREENH6);
    }];
    
    
    [self.pendingView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self);
        make.width.mas_equalTo(76 * SCREENH6);
        make.height.mas_equalTo(30 * SCREENH6);
        make.top.mas_equalTo(self).offset(442 * SCREENH6);
    }];

    
}


- (void)didRightImageButton:(UIButton *)button{
    
    NSString * buttonTag = [NSString stringWithFormat:@"%ld",(long)button.tag];
    
    NSDictionary * dict = @{@"buttonTag":buttonTag};
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"didImageButton" object:@"buttonTag" userInfo:dict];
}


- (void)pushToPendingVC{
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"pushToPendingVC" object:nil];
    
}

@end
